#include "Game.h"
#include "Utils.h"
#include "Audio.h"

Game::Game() {
	scene = new Scene();
	controller = new Controller();
	camera = new Camera( 800, 600, this );
}
Game::~Game() {
}

int Game::run()
{
	static unsigned long lastTime = 0;

	Audio::getInstance().playMusic();

	bool running = true;
	SDL_Event event;
	while ( running ) {

		unsigned long currentTime = SDL_GetTicks();
		float frametime = (currentTime - lastTime) / 1000.f;

		while ( SDL_PollEvent( &event ) )
		{
			switch ( event.type )
			{
			case SDL_KEYDOWN:
			case SDL_KEYUP:
				if( !controller->poll( event ) )
					running = false;
				break;

			case SDL_QUIT:
				running = false;
				break;

			case SDL_VIDEORESIZE:
				camera->resize( event.resize.w, event.resize.h );
			}
		};

		camera->setMovementMap( controller->getCameraMap() );
		scene->getPlayer()->setMovementMap( controller->getPlayerMap() );
	
		scene->update( frametime );
		camera->render( frametime );

		long diff = (SDL_GetTicks() - currentTime);
		if( diff < 15 )
			SDL_Delay( 15 - diff );
		
		lastTime = currentTime;
	}

	return running;
}
